import {createInertiaApp} from '@inertiajs/vue3'
import createServer from '@inertiajs/vue3/server'
import {renderToString} from '@vue/server-renderer'
import {createSSRApp, h} from 'vue'
import {resolvePageComponent} from "laravel-vite-plugin/inertia-helpers";
import {ZiggyVue} from 'ziggy-js';

const appName = 'laravel inertiajs admin';

createServer(page => createInertiaApp({
    page,
    render: renderToString,
    title: (title) => `${title} - ${appName}`,
    resolve: resolvePage,
    setup({App, props, plugin}) {
        createSSRApp({
            render: () => h(App, props),
        }).use(plugin).use(ZiggyVue)
    },
}), {cluster: true},)

function resolvePage(name) {
    const pages = import.meta.glob('../pages/**/*.vue');
    if (pages[`../pages/${name}.vue`]) {
        return resolvePageComponent(`../pages/${name}.vue`, pages);
    }
    // 添加默认返回或错误处理
    throw new Error(`Page not found: ${name}`);
}
